﻿@{
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<div id="page-header" class="clearfix">
    <div class="page-header">
        <h2>角色管理</h2>
        <span class="txt">可以增删查改一个系统角色</span>
    </div>
</div>
<div class="panel panel-default toggle panelMove panelClose panelRefresh">
    <!-- Start .panel -->
    <div class="panel-heading">
        <h4 class="panel-title">查询条件</h4>
    </div>
    <div class="panel-body pt0 pb0">
        <form class="form-horizontal group-border">
            <div class="form-group col-lg-4 col-md-6">
                <label class="col-lg-3 col-md-3 control-label" for="">角色名称</label>
                <div class="col-lg-9 col-md-9">
                    <input id="Name" type="text" class="form-control" name="default">
                </div>
            </div>
            <div class="form-group col-lg-4 col-md-6">
                <label class="col-lg-3 col-md-3 control-label" for="">角色编号</label>
                <div class="col-lg-9 col-md-9">
                    <input id="Code" type="text" class="form-control" name="default">
                </div>
            </div>
        </form>
    </div>
    <div class="panel-footer">
        <a class="btn btn-primary button-floatRight" href="@Url.Action("Create")">添加</a>
        <button id="btnReset" type="reset" class="btn btn-danger button-floatRight">重置</button>
        <button id="btnQuery" type="submit" class="btn btn-primary button-floatRight">查询</button>
    </div>
</div>

<div class="panel panel-default toggle panelMove panelClose panelRefresh">
    <div class="panel-heading">
        <h4 class="panel-title">查询结果</h4>
    </div>
    <div class="panel-body">
        <table id="RoleTable" class="table table-striped table-hover dt-responsive display nowrap" cellspacing="0" width="100%"></table>
    </div>
</div>
<script>
    $(document).ready(function () {
        Roles.Search();

        $("#btnQuery").click(function () {
            Roles.Search();
        });

        $("#btnReset").click(function () {
            $("form").each(function () {
                this.reset();
            });
        });
    });

    var Roles = {
        Search: function () {
            $("#RoleTable").mainToolDataTable({
                "ajax": {
                    url: '@Url.Action("PageRoles")',
                    type: 'POST',
                    data: { filter: { Code: $("#Code").val(), Name: $("#Name").val() } }
                },
                "columns": [
                    { "data": "Name", "title": "角色名称" },
                    { "data": "Code", "title": "角色编码" },
                    { "data": "Description", "title": "角色描述" },
                    {
                        title: "操作",
                        orderable: false,
                        data: "Id",
                        style: "min-width:190px;",
                        render: function (data, type, row) {
                            return "<a href='@Url.Action("Details")?id=" + data + "'>查看</a>&nbsp;&nbsp;" +
                            "<a href='@Url.Action("Edit")?id=" + data + "'>修改</a>&nbsp;&nbsp;" +
                            "<a href='javascript:void(0)' data-href='@Url.Action("Delete")?id=" + data + "' onclick='Roles.Delete(this);'>删除</a>&nbsp;&nbsp;" +
                            "<a href='javascript:void(0)' data-href='@Url.Action("AllocateUsers")?id=" + data + "' onclick='Roles.AllocateUsers(this);'>分配用户</a>&nbsp;&nbsp;" +
                            "<a href='javascript:void(0)' data-href='@Url.Action("ResourcePermission")?id=" + data + "' onclick='Roles.ResourcePermission(this);'>资源权限</a>";
                        }
                    }
                ]
            });
        },
        AllocateUsers: function (e) {
            $.mainTool.ajaxModal($(e).attr("data-href"), "分配用户", function () {
                Roles.AllocateUsersSubmit();
            })
        },
        ResourcePermission:function(e)
        {
            $.mainTool.ajaxModal($(e).attr("data-href"), "资源权限", function () {
                Roles.ResourcePermissionSubmit();
            })
        },
        Delete: function (e, code) {
            $.mainTool.confirm("是否删除该资源？", function (result) {
                $.ajax({
                    type: 'post',
                    url: $(e).attr("data-href"),
                    dataType: "json",
                    data: {
                        code: code,
                    },
                    success: function (data) {
                        Roles.Search();
                    },
                    error: function (e) {
                        alert(e.responseText);
                    }
                });
            });
        }
    };
</script>
